﻿using System.Collections.Generic;
using System.Linq;
using Oracle.DataAccess.Client;
using Wicresoft.SelfService.BL.Interface;
using Wicresoft.SelfService.DAO;
using Wicresoft.SelfService.DAL;

namespace Wicresoft.SelfService.BL
{
    public class BreakDown : IBreakDown, System.IDisposable
    {
        private ITServiceDAL itService;
        public BreakDown()
        {
            if (itService == null) itService = new ITServiceDAL();
        }
        public List<BreakDownList> GetTopCategory()
        {
            QueryParameters[] bdlParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="parentID", OracleDbType=OracleDbType.Int32, OracleDbValue=null,QueryOperator=QueryOperatorType.isNull, PredicateType=PredicateType.where}
            };
            List<BreakDownList> breakdownList = itService.getBreakDownList_By_QueryParams(bdlParam, null, null, null);
            return breakdownList;
        }
        public List<BreakDownList> GetTopCategory_By_Name(string name)
        {
            QueryParameters[] bdlParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="parentID", OracleDbType=OracleDbType.Int32, OracleDbValue=null,QueryOperator=QueryOperatorType.isNull, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName="LVLNAME", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=name.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            List<BreakDownList> breakdownList = itService.getBreakDownList_By_QueryParams(bdlParam, null, null, null);
            return breakdownList;
        }
        public List<BreakDownList> GetChildCategory(int ID)
        {
            QueryParameters[] bdlParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="parentID", OracleDbType=OracleDbType.Int32, OracleDbValue=ID,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            List<BreakDownList> List = new List<BreakDownList>(), tempList = new List<BreakDownList>();
            BreakDownList item = new BreakDownList();
            item.ID = 0;
            item.LvlName = "请选择...";
            item.parent = null;
            item.parentID = 0;
            List.Add(item);
            tempList = itService.getBreakDownList_By_QueryParams(bdlParam, null, null, null);
            foreach (BreakDownList myItem in tempList)
            {
                List.Add(myItem);
            }
            return List;
        }
        public List<BreakDownList> GetChildCategory_By_Name(int ID, string name)
        {
            QueryParameters[] bdlParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="parentID", OracleDbType=OracleDbType.Int32, OracleDbValue=ID,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName="LVLNAME", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=name.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            List<BreakDownList> List = itService.getBreakDownList_By_QueryParams(bdlParam, null, null, null);
            if (List.Count == 0)
            {
                BreakDownList item = new BreakDownList();
                item.ID = 0;
                item.LvlName = "请选择...";
                item.parent = null;
                item.parentID = 0;
                List.Add(item);
            }
            return List;
        }
        public BreakDownList GetBreakDownInfo(int ID)
        {
            QueryParameters[] bdlParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="ID", OracleDbType=OracleDbType.Int32, OracleDbValue=ID,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            List<BreakDownList> bdInfo = itService.getBreakDownList_By_QueryParams(bdlParam, null, null, null);
            return bdInfo.FirstOrDefault();
        }

        public void Dispose()
        {
            if (this.itService != null)
                this.itService.Dispose();
        }
    }
}
